jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.page.serviceView.tabContent.providesConsumes.ProvidesConsumes", //the full name of the class to create
        jsx3.lang.Object, //  extends
        [tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceTabInterface], // implements
        function (plugin) {
        	plugin.serviceCacheId = "SPM_PROVIDES_CONSUMES_SERVICE_CACHE";
        	plugin.getServiceIdSubject = "com.tibco.admin.monitormanage.uicomponents.page.serviceView.tabContent.providesConsumes.ProvidesConsumes.getServiceId";
        	plugin.handleInitialSelectSubject = "tibco.admin.monitormanage.uicomponents.page.serviceView.tabContent.providesConsumes.ProvidesConsumes.handleInitialSelect";
        	
        	plugin.prototype.serviceId;
        	plugin.prototype.providesConsumesBlock;

	    plugin.prototype.componentLoaded = false;
        
            plugin.instance = null;

            plugin.getInstance = function(args) {
                if(!this.instance) {
                    this.instance = new tibco.admin.monitormanage.uicomponents.page.serviceView.tabContent.providesConsumes.ProvidesConsumes();
                }
                return this.instance;
            }

            plugin.prototype.getNamespace = function() {
               return "tibco.admin";
            }
            
            plugin.prototype.getTabText = function() {
				return tibco.admin.Main.getServer().getDynamicProperty("@MM@general@ProvidesAndConsumesTitle");
            	//return "Provides & Consumes";
            }
            
            plugin.prototype.updateComponent = function(paramArray) {
            	this.serviceId = paramArray.serviceId;
            	var message = { serviceId:this.serviceId };
            	tibco.admin.monitormanage.general.PageBusUtils.publish(plugin.getServiceIdSubject, message);
            	
            	// clear the provides/consumes services tables
            	if(this.componentLoaded)
            	{
            		var message = {interfaceId:null};
					tibco.admin.monitormanage.general.PageBusUtils.publish(tibco.admin.monitormanage.uicomponents.table.providesConsumesInterfaces.ProvidesConsumesInterfaces.interfaceSelectedSubject, message);
		this.handleVisibility();
				}
            	
            	if(paramArray.consumerProviderKey)
            	{
            		// must have navigated from the Provides/Consumes tab of another service
            		//alert(paramArray.consumerProviderKey + " and isInterfaceConsumersMode = " + paramArray.isInterfaceConsumersMode);
            		
            		// TODO: May have to uncomment this later if we don't persist the tab state
					// show this tab
					//var tabId = 'service' + 'tab' + this.getTabText();
					//this.providesConsumesBlock.getAncestorOfName(tabId).doShow();
					
					message = { consumerProviderKey:paramArray.consumerProviderKey, isInterfaceConsumersMode:paramArray.isInterfaceConsumersMode};
					tibco.admin.monitormanage.general.PageBusUtils.publish(plugin.handleInitialSelectSubject, message);
            	}
            }
            
            plugin.prototype.tabClicked = function(objEvent) {
			if(this.componentLoaded)
			{ this.handleVisibility(); }
	        }

            plugin.prototype.loadComponent = function(parent) {
				var server = tibco.admin.Main.getServer();
                this.providesConsumesBlock = tibco.admin.Main.loadUIResource(parent, this.getNamespace(), "tibco/admin/monitormanage/uicomponents/page/serviceView/tabContent/providesConsumes/components/providesConsumes.xml", true);

                var providesLeftPane = this.providesConsumesBlock.getDescendantOfName("topLeftPane");
        		if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.uicomponents || !tibco.admin.monitormanage.uicomponents.table || !tibco.admin.monitormanage.uicomponents.table.providesConsumesInterfaces || !tibco.admin.monitormanage.uicomponents.table.providesConsumesInterfaces.ProvidesConsumesInterfaces)
                { tibco.admin.Main.loadClass("tibco/admin/monitormanage/uicomponents/table/providesConsumesInterfaces/ProvidesConsumesInterfaces.js", this.getNamespace()); }
                var providesInterfacesObj = tibco.admin.Main.loadUIResource(providesLeftPane, this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/providesConsumesInterfaces/components/providesConsumesInterfaces.xml", true);
                var providesTitle = providesInterfacesObj.getDescendantOfName("titleLabel");
                providesTitle.setText(server.getDynamicProperty("@MM@general@pvcProvidesTitle"), true);
                providesInterfacesObj.setUpComponent("_provides_int_MM_svc", true, this.serviceId);
                
                var providesRightPane = this.providesConsumesBlock.getDescendantOfName("topRightPane");
        		if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.uicomponents || !tibco.admin.monitormanage.uicomponents.table || !tibco.admin.monitormanage.uicomponents.table.providesConsumesServices || !tibco.admin.monitormanage.uicomponents.table.providesConsumesServices.ProvidesConsumesServices)
                { tibco.admin.Main.loadClass("tibco/admin/monitormanage/uicomponents/table/providesConsumesServices/ProvidesConsumesServices.js", this.getNamespace()); }
                var providesServicesObj = tibco.admin.Main.loadUIResource(providesRightPane, this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/providesConsumesServices/components/providesConsumesServices.xml", true);
                var providesServicesTitle = providesServicesObj.getDescendantOfName("titleLabel");
                providesServicesTitle.setText(server.getDynamicProperty("@MM@general@pvcProvidesServicesTitle"), true);
                providesServicesObj.setUpComponent("_provides_svc_MM_svc", true);
                
                var consumesLeftPane = this.providesConsumesBlock.getDescendantOfName("bottomLeftPane");
        		if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.uicomponents || !tibco.admin.monitormanage.uicomponents.table || !tibco.admin.monitormanage.uicomponents.table.providesConsumesInterfaces || !tibco.admin.monitormanage.uicomponents.table.providesConsumesInterfaces.ProvidesConsumesInterfaces)
                { tibco.admin.Main.loadClass("tibco/admin/monitormanage/uicomponents/table/providesConsumesInterfaces/ProvidesConsumesInterfaces.js", this.getNamespace()); }
                var consumesInterfacesObj = tibco.admin.Main.loadUIResource(consumesLeftPane, this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/providesConsumesInterfaces/components/providesConsumesInterfaces.xml", true);
                var consumesTableTitle = consumesInterfacesObj.getDescendantOfName("titleLabel");
                consumesTableTitle.setText(server.getDynamicProperty("@MM@general@pvcConsumesTable"), true);
                consumesInterfacesObj.setUpComponent("_consumes_int_MM_svc", false, this.serviceId);
                
                var consumesRightPane = this.providesConsumesBlock.getDescendantOfName("bottomRightPane");
        		if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.uicomponents || !tibco.admin.monitormanage.uicomponents.table || !tibco.admin.monitormanage.uicomponents.table.providesConsumesServices || !tibco.admin.monitormanage.uicomponents.table.providesConsumesServices.ProvidesConsumesServices)
                { tibco.admin.Main.loadClass("tibco/admin/monitormanage/uicomponents/table/providesConsumesServices/ProvidesConsumesServices.js", this.getNamespace()); }
                var consumesServicesObj = tibco.admin.Main.loadUIResource(consumesRightPane, this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/providesConsumesServices/components/providesConsumesServices.xml", true);
                var consumesServicesTableTitle = consumesServicesObj.getDescendantOfName("titleLabel");
                consumesServicesTableTitle.setText(server.getDynamicProperty("@MM@general@pvcConsumesServicesTable"), true);
                consumesServicesObj.setUpComponent("_consumes_svc_MM_svc", false);

		this.handleVisibility();

		this.componentLoaded = true;
            }
            
            //call this method to begin the service call (eg.service.callgetServices();)
    		plugin.prototype.callGetServices = function() {
    			var rulesPath = "tibco/admin/monitormanage/uicomponents/page/serviceView/tabContent/providesConsumes/rules/serviceRules.xml";
    			var dataPath = "tibco/admin/monitormanage/uicomponents/page/serviceView/tabContent/providesConsumes/data/serviceData.xml";
    			var operation = "getServices";
    			if (tibco.admin.Main.getServer().getEnv("MODE")) {
                    // mode is live
                    var objService = tibco.admin.Main.loadService(this.getNamespace(), rulesPath, operation);
                    objService.setEndpointURL(plugin.wsEndpointURL);
		    		// this setInboundURL should not be required...only here because of a current AMX framework bug
		    		objService.setInboundURL(dataPath);
                }
                else {
                    var objService = new jsx3.net.Service(tibco.admin.Main.resolveURI(this.getNamespace(), rulesPath), operation, null, null);
                    // mode is static
                    objService.setInboundURL(tibco.admin.Main.resolveURI(this.getNamespace(), dataPath));
                }
                
                objService.providesConsumesObj = this;
                objService.serviceId = this.serviceId;
                
                // initialize the list of metrics we wish to retrieve data for
				var metricsList = new jsx3.util.List();
				metricsList.add("resourceType");
				metricsList.add("externalEndpoint");
				objService.metricsList = metricsList;

		    	//subscribe and call
  		        objService.subscribe(jsx3.net.Service.ON_SUCCESS, this.ongetServicesSuccess);
			    objService.subscribe(jsx3.net.Service.ON_ERROR, this.ongetServicesError);
			    objService.subscribe(jsx3.net.Service.ON_INVALID, this.ongetServicesInvalid);
			    objService.doCall();
		    };

    		plugin.prototype.handleVisibility = function() {
      			
      			var serviceCacheDoc = tibco.admin.Main.getServer().Cache.getDocument(tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceInfoCacheId);
      			var record = serviceCacheDoc.getFirstChild();
      			var resourceType = record.getAttribute("resourceType");

      			var providesConsumesBlock = this.providesConsumesBlock;
      			
				var resourceTypeFound = false;
      			var topTablesDisplayMode;
      			var bottomTablesDisplayMode;
      			var topExtEndpointDisplayMode;
      			var bottomExtEndpointDisplayMode;
      			if(resourceType == "COMPOSITE_SERVICE")
      			{
      				resourceTypeFound = true;
      				topTablesDisplayMode = jsx3.gui.Block.DISPLAYNONE;
      				bottomTablesDisplayMode = jsx3.gui.Block.DISPLAYBLOCK;
      				topExtEndpointDisplayMode = jsx3.gui.Block.DISPLAYBLOCK;
      				bottomExtEndpointDisplayMode = jsx3.gui.Block.DISPLAYNONE;
      			}
      			else if(resourceType == "COMPONENT")
      			{
      				resourceTypeFound = true;
      				topTablesDisplayMode = jsx3.gui.Block.DISPLAYBLOCK;
      				bottomTablesDisplayMode = jsx3.gui.Block.DISPLAYBLOCK;
      				topExtEndpointDisplayMode = jsx3.gui.Block.DISPLAYNONE;
      				bottomExtEndpointDisplayMode = jsx3.gui.Block.DISPLAYNONE;
      			}
      			else if(resourceType == "COMPOSITE_REFERENCE")
      			{
      				resourceTypeFound = true;
      				topTablesDisplayMode = jsx3.gui.Block.DISPLAYBLOCK;
      				bottomTablesDisplayMode = jsx3.gui.Block.DISPLAYNONE;
      				topExtEndpointDisplayMode = jsx3.gui.Block.DISPLAYNONE;
      				bottomExtEndpointDisplayMode = jsx3.gui.Block.DISPLAYBLOCK;
      			}
      			else
      			{ alert("ERROR: Could not recognize resourceType of \"" + resourceType + "\" in tibco.admin.monitormanage.uicomponents.page.serviceView.tabContent.providesConsumes.ProvidesConsumes"); }

				if(resourceTypeFound)
				{
					// handle the top splitter
					var topSplitter = providesConsumesBlock.getDescendantOfName("topSplitter");
      				if(topSplitter.getDisplay() != topTablesDisplayMode)
      				{ topSplitter.setDisplay(topTablesDisplayMode, true); }
					
					// handle the top endpoint field
					var extEndpointBlock = providesConsumesBlock.getDescendantOfName("topExtEndpointBlock");
					if(topExtEndpointDisplayMode == jsx3.gui.Block.DISPLAYBLOCK)
					{
      					var extEndpointLabel = extEndpointBlock.getDescendantOfName("topExtEndpointValue");
      					var extEndpoint = record.getAttribute("externalEndpoint");
      					extEndpointLabel.setText(extEndpoint, true);
						extEndpointBlock.getParent().getParent().setSubcontainer1Pct("10%", true);
      				} else { extEndpointBlock.getParent().getParent().setSubcontainer1Pct("50%", true); }
					
      				if(extEndpointBlock.getDisplay() != topExtEndpointDisplayMode)
      				{ extEndpointBlock.setDisplay(topExtEndpointDisplayMode, true); }

					// handle the bottom splitter
					var bottomSplitter = providesConsumesBlock.getDescendantOfName("bottomSplitter");
      				if(bottomSplitter.getDisplay() != bottomTablesDisplayMode)
      				{ bottomSplitter.setDisplay(bottomTablesDisplayMode, true); }

					// handle the bottom endpoint field
					var extEndpointBlock = providesConsumesBlock.getDescendantOfName("bottomExtEndpointBlock");
					if(bottomExtEndpointDisplayMode == jsx3.gui.Block.DISPLAYBLOCK)
					{
      					var extEndpointLabel = extEndpointBlock.getDescendantOfName("bottomExtEndpointValue");
      					var extEndpoint = record.getAttribute("externalEndpoint");
      					extEndpointLabel.setText(extEndpoint, true);
      				}
      				if(extEndpointBlock.getDisplay() != bottomExtEndpointDisplayMode)
      				{ extEndpointBlock.setDisplay(bottomExtEndpointDisplayMode, true); }
				}
    		};

    		plugin.prototype.ongetServicesError = function(objEvent) {
      			var myStatus = objEvent.target.getRequest().getStatus();
      			jsx3.log("Error","The service call failed. The HTTP Status code is: " + myStatus);
    		};

    		plugin.prototype.ongetServicesInvalid = function(objEvent) {
      			jsx3.log("Invalid","The following message node just failed validation:\n\n" + objEvent.message);
    		};


            

        });